##########################
# Nadiya Kostyuk
# Creating plots
# R version: 4.1
# 11/12/25
##########################

rm(list=ls())

## Install & load packages (all at once)
list.of.packages <- c('dplyr', 'ggplot2', 'fuzzyjoin')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]; if(length(new.packages)){install.packages(new.packages,dependencies=TRUE)}
lapply(list.of.packages, require, character.only = TRUE)

# setting directory: 
setwd("")

# loading daily data: 
load('data/data_final.RData')


data_final = data_final %>% 
  mutate(
    war_phases_new = case_when(
      war_phases == 'Rus Maneuver Offensive' & territorial_control_80 == 'Contested' ~ 'Rus Maneuver Offensive',
      war_phases == 'Rus Maneuver Defensive' & territorial_control_80 == 'Contested' ~ 'Rus Maneuver Defensive',
      war_phases == 'Attrition' & territorial_control_80 == 'Contested' ~ 'Attrition', 
      territorial_control_80 == 'Russian occupation' ~ 'Russian occupation',
      .default = 'Punishment'
    )
  )


##############################
# Figure 1
##############################

# Ensure DATE is a Date type
df <- data_final %>%
  mutate(DATE = as.Date(DATE)) %>% 
  #filter(ADM1_NAME == 'Donetsk') %>% 
  # selecting required cells:
  dplyr::select(ADM1_NAME, DATE, 
                war_phases_new,
                disruption_intentional_3indicators, disruption_intentional_3indicators_1dlag)
head(df)


phase_df <- df %>%
  arrange(ADM1_NAME, DATE) %>%
  group_by(ADM1_NAME) %>%
  mutate(
      phase_change = war_phases_new != dplyr::lag(war_phases_new, default = first(war_phases_new)),
    segment_id = cumsum(phase_change)
  ) %>%
  group_by(ADM1_NAME, segment_id, war_phases_new) %>%
  summarise(
    start = min(DATE),
    end   = max(DATE),
    .groups = "drop"
  ) %>%
  arrange(ADM1_NAME, start)
phase_df


df_joined <- fuzzy_left_join(
  df,
  phase_df,
  by = c(
    "ADM1_NAME" = "ADM1_NAME",
    "DATE"   = "start",
    "DATE"   = "end"
  ),
  match_fun = list(`==`, `>=`, `<=`)
) %>%
  dplyr::select(-c(ADM1_NAME.y, war_phases_new.y)) %>%
  dplyr::rename(
    ADM1_NAME = ADM1_NAME.x,
    war_phases_new = war_phases_new.x
  )
head(df_joined)
tail(df_joined)


df_phase_summary <- df_joined %>%
  group_by(ADM1_NAME, segment_id, war_phases_new) %>%
  summarise(
    outages_total = sum(disruption_intentional_3indicators == 1, na.rm = TRUE),
    start = min(DATE),
    end   = max(DATE),
    .groups = "drop"
  )

head(df_phase_summary)
df_phase_summary %>% as.data.frame()


df_phase_summary <- df_phase_summary %>%
  mutate(
    war_phases_new = recode(
      war_phases_new,
      "Rus Maneuver Offensive" = "Rus Offensive Maneuver",
      "Rus Maneuver Defensive" = "Rus Defensive Maneuver"
    ),
    war_phases_new = factor(
      war_phases_new,
      levels = c("Rus Offensive Maneuver", "Attrition", "Rus Defensive Maneuver", "Punishment", 'Russian occupation')
    )
  )


phase_colors <- c(
  "Rus Offensive Maneuver" = "black",
  "Attrition"              = "grey30",
  "Rus Defensive Maneuver" = "grey50",
  "Punishment"             = "grey70", 
  # get rid of white
  'Russian occupation' = "grey90"
)

x_breaks <- df_phase_summary$start

df_phase_summary <- df_phase_summary %>%
  mutate(
    mid_date = start + (end - start)/2
  )


data_final_trimmed = data_final %>%
  filter(disruption_intentional_3indicators == 1 
         & disruption_intentional_3indicators_1dlag == 0)


###############################################

ggplot(df_phase_summary) +
  geom_rect(
    aes(xmin = start, xmax = end, 
        ymin = -Inf, ymax = Inf, fill = war_phases_new)
  ) +
  geom_rug(data = data_final_trimmed, 
           aes(x = DATE), 
           length = unit(0.25, "npc"), 
                     outside = FALSE, alpha = 1, color = 'white') + 
  facet_wrap(~ ADM1_NAME, ncol = 5) +
  scale_fill_manual(values = phase_colors) +
  scale_x_date(
    date_labels = "%Y-%m",
    date_breaks = "3 months" 
    #limits = c(mdy('02-24-2022'), mdy('12-31-2023'))
  ) +
  labs(
    x = "Date",
    y = "Total outage days per phase segment",
    fill = "War phase"
  ) +
  # theme_bw() + 
  theme_minimal(base_size = 11) +
  theme(
    panel.grid = element_blank(),
    strip.text = element_text(face = "bold"),
    legend.position = "bottom",
    axis.text.x = element_text(angle = 75, hjust = 1),
    axis.text.y = element_blank()
  ) + 
  ylim(0, 1) 
#ggsave("analysis/output/figures/rusoutages_byphase_byoblast.pdf", width = 8, height = 6)







